<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <div class="table-operator" style="margin-top: 5px">
        <a-button @click="AddContacts" type="primary" icon="plus">新增</a-button>
      </div>
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :scroll="scroll"
          :loading="loading"
          @change="handleTableChange"
        >
          <span slot="action" slot-scope="text, record">
            <a-popconfirm title="确定删除吗?" @confirm="() => myhandleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span slot="fileUrl" slot-scope="text, record">
            <a @click="myhandleExportXls(record)">下载</a>
            <a-divider type="vertical" />
            <a @click="previewFile(record)">预览</a>
          </span>
          <!-- 状态渲染模板 -->
          <template slot="customRenderFlag" slot-scope="enabled">
            <a-tag v-if="enabled" color="green">启用</a-tag>
            <a-tag v-if="!enabled" color="orange">禁用</a-tag>
          </template>
        </a-table>
      </div>
    </a-spin>
    <addHandleSee ref="modalForm" @ok="remodalFormOk"></addHandleSee>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addCustomer, editSupplier, checkSupplier } from '@/api/api'
import { getAction, deleteAction, getActionImg } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import addHandleSee from './addHandleSee'
import { downFile } from '@/api/manage'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'ContactsModal',
  mixins: [JeecgListMixin],
  components: {
    addHandleSee
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        { title: '任务单号', dataIndex: 'taskCode', width: 70, align: 'center' },
        { title: '内部编码', dataIndex: 'contractCode', width: 70, align: 'center' },
        {
          title: '任务单文件',
          dataIndex: 'fileUrl',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'fileUrl' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 200,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/productionTask/getTaskList',
        delete: '/productionTask/delete',
        exportXlsUrl: '/systemConfig/static'
      }
    }
  },
  created() {},
  methods: {
    previewFile(record) {
      console.log(record)
      if (record && record.fileUrl) {
        let params = record.fileUrl
        getActionImg('systemConfig/preview/' + params).then(res => {
          console.log(res)
          const binaryData = []
          binaryData.push(res)
          window.open(window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' })))
        })
      }
    },
    myhandleExportXls(record) {
      downFile(this.url.exportXlsUrl + '/' + record.fileUrl).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), record.fileUrl)
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', record.fileUrl)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    myhandleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { testId: id, id: id }).then(res => {
        if (res.code === 200 || res.code === 10000) {
          that.$message.success('删除成功')
          that.getActionData()
        } else {
          that.$message.warning(res.data.message)
        }
      })
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      if (pagination && pagination.current) {
        this.ipagination = pagination
      }
      this.getActionData()
    },
    remodalFormOk() {
      this.getActionData()
    },
    AddContacts() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.record = this.record
      this.$refs.modalForm.disableSubmit = false
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.record = record
      this.getActionData()
    },
    getActionData() {
      var params = {
        contractCode: this.record.contractCode
      }
      getAction(this.url.list, params).then(res => {
        if (res.code === 200 || res.code === 10000) {
          this.dataSource = res.data.records
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          // formData.type = '客户'
          formData.entryTime = this.$moment(formData.entryTime).format('YYYY-MM-DD')
          let obj
          if (!this.model.id) {
            obj = addCustomer(formData)
          } else {
            obj = addCustomer(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.data.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped>
/** Button按钮间距 */
.table-operator .ant-btn {
  margin: 0 8px 8px 0;
}
</style>
